/**
题目:
    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.

Example:

Input: 3
Output:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3
思路:
    递归,左右组合join思维
效果:
    执行用时：4 ms, 在所有 Go 提交中击败了74.95%的用户
    内存消耗：4.4 MB, 在所有 Go 提交中击败了36.99%的用户
*/
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func generateTrees(n int) []*TreeNode {
    if n  == 0{
        return nil
    }
    return helper(1,n)
}

func helper(start, end int)[]*TreeNode{
    if start > end{
        return []*TreeNode{nil}
    }
    res := make([]*TreeNode,0)
    for i := start; i <= end;i++{
        lefts := helper(start, i - 1)
        rights := helper(i+1, end)
        for _, l := range lefts{
            for _, r := range rights{
                node := &TreeNode{i,l,r}
                res = append(res, node) 
            }
        }
    }
    return res
}